package com.apache.flow.web;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.apache.common.DateUtil;
import com.apache.common.FlowBaseAction;
import com.apache.core.orm.Page;
import com.apache.core.web.struts2.Struts2Utils;
import com.apache.flow.core.entity.FlowSys;
import com.apache.flow.core.entity.FlowTaskUrl;
import com.apache.flow.core.manager.FlowSysManager;
import com.apache.flow.core.manager.FlowTaskUrlManager;
import com.apache.flow.vo.FlowSysQvo;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;

@Namespace("/flow/system")
@Results({})
public class FlowSysAction extends FlowBaseAction {
	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(FlowSysAction.class);
	/**
	 * 分页：每页显示的记录数
	 */
	private String rows;
	/**
	 * 分页：当前页号
	 */
	private String page;

	private String sysCname;
	private String sysEname;

	/** 是否是提交表单，T：提交，其他：非提交 */
	private String submitFlag;
	/**
	 * id
	 */
	private String sysId;
	/**
	 * 资源entity
	 */
	private FlowSys flowSys;

	/**
	 * 权限系统注册service
	 */
	@Autowired
	private FlowSysManager flowSysManager;

	@Autowired
	private FlowTaskUrlManager flowTaskUrlManager;

	/**
	 * 跳转到页面
	 */
	public String execute() throws Exception {
		return SUCCESS;
	}

	/**
	 * 取到权限系统注册分页信息
	 * @return String
	 * @throws Exception 
	 */
	public String data() throws Exception {
		int pageNo = NumberUtils.getInt(page, 1);
		int pageSize = NumberUtils.getInt(rows, 15);
		FlowSysQvo qvo = new FlowSysQvo();
		qvo.setSysCname(sysCname);
		qvo.setSysEname(sysEname);
		Page<FlowSys> sysPage = flowSysManager.getSysListByQvo(qvo, pageNo, pageSize);
		JSONObject jobj = new JSONObject();
		jobj.put("total", sysPage.getTotalCount());
		jobj.put("rows", JSONArray.fromObject(sysPage.getResult()));
		Struts2Utils.getResponse().getWriter().write(jobj.toString());
		return null;
	}

	/**
	 * 执行删除
	 * @return String
	 * @throws Exception
	 */
	public String delete() throws Exception {
		logger.info("删除系统信息   sysId=" + sysId);
		if (StrUtil.isNotNull(sysId)) {
			List<FlowTaskUrl> list = flowTaskUrlManager.getFlowTaskUrlBySysId(sysId);
			if (list.size() > 0) {
				return this.showError("该系统下已存在资源,请先删掉该系统下的资源");
			} else {
				FlowSys flowSys = flowSysManager.getFlowSys(sysId);
				flowSys.setSysStatus("0");
				flowSysManager.updateFlowSys(flowSys);
				return this.showOk("删除成功");
			}
		} else {
			this.showError("删除失败");
		}
		return null;
	}

	/** 
	 * 保存入库 
	 * @return String
	 * @throws Exception
	 **/
	public String save() throws Exception {
		logger.info("添加或修改系统信息   sysId=" + sysId);
		String userName = loginUser.getUserEname();
		if (StrUtil.isNotNull(sysId)) {
			FlowSys sys = flowSysManager.getFlowSys(sysId);
			sys.setSysCname(flowSys.getSysCname().trim());
			sys.setSysEname(flowSys.getSysEname().trim());
			sys.setSysUrl(flowSys.getSysUrl().trim());
			sys.setCreateUser(userName);
			flowSysManager.updateFlowSys(sys);
		} else {
			//校验是否已经注册了
			String ename = StrUtil.doNull(flowSys.getSysEname(), "");

			FlowSys sys = flowSysManager.getFlowSysListBypropertyName("sysEname", ename);
			if (sys != null) {//已经存在
				this.showError("该权限英文标识已经存在，请换一个");
				return null;
			}

			flowSys.setCreateUser(userName);
			flowSys.setCreateTime(DateUtil.getNowTime());
			flowSys.setSysStatus("1");

			//保存
			flowSysManager.saveSys(flowSys);
		}
		this.showOk("操作成功");
		return null;
	}

	public String toSave() throws Exception {
		logger.info("跳转到添加或修改系统界面   sysId=" + sysId);
		if (StrUtil.isNotNull(sysId)) {
			flowSys = flowSysManager.getFlowSys(sysId);
		}
		return "save";
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getSysCname() {
		return sysCname;
	}

	public void setSysCname(String sysCname) {
		this.sysCname = sysCname;
	}

	public String getSysEname() {
		return sysEname;
	}

	public void setSysEname(String sysEname) {
		this.sysEname = sysEname;
	}

	public String getSubmitFlag() {
		return submitFlag;
	}

	public void setSubmitFlag(String submitFlag) {
		this.submitFlag = submitFlag;
	}

	public String getSysId() {
		return sysId;
	}

	public void setSysId(String sysId) {
		this.sysId = sysId;
	}

	public FlowSys getFlowSys() {
		return flowSys;
	}

	public void setFlowSys(FlowSys flowSys) {
		this.flowSys = flowSys;
	}

}
